
var bsurl = require('../../../../utils/baseUrl.js');
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    recname:"",
    phone:"",
    addr:"",
    isNew:true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    if(!options.recname){
      return;
    }
    this.setData({
      id:options.id,
      recname:options.recname,
      phone:options.phone,
      addr:options.addr,
      isNew:false
    });
  },
  textinput: function (event) {
    var type = event.currentTarget.dataset.type;
    switch (Number(type)) {
      case 1: this.setData({ recname: event.detail.value }); break;
      case 2: this.setData({ phone: event.detail.value }); break;
      case 3: this.setData({ addr: event.detail.value }); break;
    }
  },
  save:function(){
    // var str = "";
    // if(this.data.recname == ""){
    //   str="收货人姓名不能为空";
    // }
    // if (!/^((13[0-9])|(14[5|7])|(15([0-3]|[5-9]))|(18[0,5-9]))\d{8}$/.test(this.data.phone)) {
    //   str = '手机号格式不对';
    // }
    // if (this.data.addr == "") {
    //   str = "地址不能为空";
    // }
    // if(str != ""){
    //   //提示
    //     return;
    // }
    var url = "buyer/addAddress.do";
    var addr = {
      recname: this.data.recname,
      phone: this.data.phone,
      addr: this.data.addr
    };
    if(!this.data.isNew){
      url = "buyer/updateAddress.do";
      addr["id"] = this.data.id;
    }
    wx.showToast({
      title: '保存中...',
      icon: "loading",
      duration: 150000
    })
    wx.request({
      url: bsurl + url,
      method: "post",
      data: {
        body:addr
      },
      header: {
        'content-type': 'application/json', // 默认值
        JSESSIONID: app.globalData.JSESSIONID
      },
      success: function (res) {
        wx.hideToast();
        if (res.statusCode != 200) {
          wx.showModal({
            title: '提示',
            content: "请求失败",
          });
          return;
        }
        console.log(res);
        if (res.data.success) {
          wx.showToast({
            title: '保存成功'
          });
          wx.navigateBack({
            delta:1
          })
        }
        else {
          wx.showModal({
            title: '提示',
            content: res.data.message,
          });
        }
      },
      fail: function (res) {
        wx.hideToast();
        wx.showModal({
          title: '提示',
          content: "网络请求失败",
        });
      }
    })
  }
})